/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ourgame.tankgame.control;

import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
//import com.ourgame.tankgame.bullet.Bullet;
import com.jme3.scene.control.Control;
import com.ourgame.tankgame.attribute.BulletAttribute;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author echoff
 */
public abstract class WeaponSystem extends AbstractControl {

    protected int bulletsMaxNum;//弹仓容弹量
    protected int reloadTime;//子弹装填时间
    protected int bulletTypesMax;//武器系统最多可装备子弹种类数
    protected int initBulletsNum = 100;//装备一类子弹是默认装备的数量
    protected int bulletloadSate;//子弹装填状态 0装填好 1正在装填 2未装填
    
//    protected Bullet currentBullet;//当前使用的子弹
    protected BulletAttribute currentBulletAttribute;
    protected Map<BulletAttribute,Integer> bulletsNum = new HashMap<BulletAttribute, Integer>();//当前每种子弹对应的子弹数量
    
    protected List<BulletAttribute> bulletTypeList = new ArrayList<BulletAttribute>();//子弹种类列表
    
    protected ControlSystem controler;
    
    //********************************************************
    //*      get、set 区域                                    *
    //********************************************************

    public int getReloadTime() {
        return reloadTime;
    }

    public void setReloadTime(int reloadTime) {
        this.reloadTime = reloadTime;
    }
    //********************************************************
    //*      功能方法区域                                     *
    //********************************************************
    
    public abstract void initWeaponSystem();
    
    /**
     * 装备一种子弹到武器系统 
     * @param bullet
     * @return 
     */
    public boolean equipBullet(BulletAttribute bullet) {
        if (!bulletTypeList.contains(bullet)) {
            bulletTypeList.add(bullet);
            bulletsNum.put(bullet, initBulletsNum);
            currentBulletAttribute = bullet;
            return true;
        } else {
            return false;
        }
    }
    /**
     * 从武器系统中卸下一种子弹
     * @param bullet
     * @return 
     */
    public boolean unequipBullet(BulletAttribute bullet){
        if (bulletTypeList.contains(bullet)){
            bulletTypeList.remove(bullet);
            bulletsNum.remove(bullet);
            return true;
        }else{
            return false;
        }
    }
    /**
     * 获取当前使用的子弹的数量
     * @return 
     */
    public int getCurrentBulletsNum(){
        return bulletsNum.get(currentBulletAttribute);
    }
    /**
     * 设定当前使用的子弹的数量
     * @param bullets 
     */
    public void setCurrentBulletsNum(int bullets){
        bulletsNum.put(currentBulletAttribute, bullets);
    }
    /**
     * 切换到下一种子弹
     */
    public void swichToNextBullet(){
        int index = bulletTypeList.indexOf(currentBulletAttribute);
        index ++;
        if (index == bulletTypeList.size()){
            currentBulletAttribute = bulletTypeList.get(0);
        }else{
            currentBulletAttribute = bulletTypeList.get(index);
        }
    }
    /**
     * 发射
     */
    public abstract void fire();
//    public boolean fire(){
//        System.out.println("fire on the hold!");
//        if (bulletloadSate == 0){
////            TODO 调用bullet的发射方法
//            currentBullet.fire();
//            setCurrentBulletsNum(getCurrentBulletsNum()-1);
//            return true;
//        }else {
//            return false;
//        }
//    }
    /**
     * 装填子弹
     * @return 当前装填状态 
     */
    public int reloadBullet(){
        if(bulletloadSate == 2){
//            TODO:启动装填计时器
            return 1;
        }else if(bulletloadSate == 1){
            return 1;
        }else {
            return 0;
        }
    }

    /**
     * @param controler the controler to set
     */
    public void setControler(ControlSystem controler) {
        this.controler = controler;
    }
    

    @Override
    protected void controlUpdate(float tpf) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }    
    
}
